package com.rs.game.player;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

import com.rs.utils.Utils;

public class IntervalTimer implements Serializable {

	private static final long serialVersionUID = 7008928474981536265L;
	private long totaltime, startintervaltime;
	
	private long getTotalTime() 
	{
		return totaltime;
	}

	private void setTotalTime(long totaltime) 
	{
		this.totaltime = totaltime;
	}

	public void UpdateTotalTime() 
	{
		setTotalTime(getTotalTime() + (Utils.currentTimeMillis() - startintervaltime));
		go();
	}

	public long getIntervalTime()
	{
		long intervaltime = (Utils.currentTimeMillis() - startintervaltime);
		return intervaltime;
	}

	public String getPlayTime()
	{
		long millis = totaltime;
		long days = TimeUnit.MILLISECONDS.toDays(millis);
		millis -= TimeUnit.DAYS.toMillis(days);
		long hours = TimeUnit.MILLISECONDS.toHours(millis);
		millis -= TimeUnit.HOURS.toMillis(hours);
		long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
		millis -= TimeUnit.MINUTES.toMillis(minutes);
		long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

		StringBuilder sb = new StringBuilder(64);
		sb.append(days);
		sb.append(" Day"+(days == 1 ? ", " : "s, "));
		sb.append(hours);
		sb.append(" Hour"+(hours == 1 ? ", " : "s, "));
		sb.append(minutes);
		sb.append(" Minute"+(minutes == 1 ? " and " : "s and "));
		sb.append(seconds);
		sb.append(" Second"+(seconds == 1 ? "." : "s."));
		return(sb.toString());
	}

	void go() 
	{
		startintervaltime = Utils.currentTimeMillis();
	}

}
